﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using webcam;
using System.IO;
using WhiteBoard;
using WFFSClients.camera;
using System.Runtime.InteropServices;//添加


namespace WFFS
{
    public partial class Camera : Form
    {
        public Camera()
        {
            InitializeComponent();
        }
       
        public Form1 form;
        VideoWork wv;
        private void Camera_Load(object sender, EventArgs e)
        {
        wv = new VideoWork(panel1.Handle, 0, 0, panel1.Width, panel1.Height);
          wv.Start();
          wv = new VideoWork(panel1.Handle, 0, 0, panel1.Width, panel1.Height);
          wv.Start();
          this.pictureBox1.Visible = false;
        }
        Bitmap Bi;
        private void button1_Click(object sender, EventArgs e)
        { if (this.button1.Text.Equals("拍照"))
            {
                button1.Text = "继续";
                String path = "c:\\a.bmp";
                wv.GrabImage(path);
                Bi = new Bitmap(path);
                this.pictureBox1.Visible = true;
                this.panel1.Visible = false;
                this.pictureBox1.Image = Bi;
                if (this.form.netWork != null){
                    System.IO.MemoryStream Ms = new MemoryStream();
                    this.pictureBox1.Image.Save(Ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                    byte[] img = new byte[Ms.Length];
                    Ms.Position = 0; Ms.Read(img, 0, Convert.ToInt32(Ms.Length));
                    Ms.Close();
                    WBMsgImageUpdate wn = new WBMsgImageUpdate();
                    wn.im = img;
                    String name = DateTime.Now.ToString("yyyyMMddHHmmss") + ".bmp"; 
                    wn.name = name;
                    form.netWork.SendWBMsgConnectedHdlr(wn);
                }
            }
            else
            {
                button1.Text = "拍照";
                this.pictureBox1.Image = null;
                Bi.Dispose();
                Bi = null;
                this.pictureBox1.Visible = false;
                this.panel1.Visible = true;
            }
        }

        private void Camera_FormClosed(object sender, FormClosedEventArgs e)
        {
            string x = this.Location.X.ToString();
            string y = this.Location.Y.ToString();
            WriteIniData("FrmZC", "X", x, Application.StartupPath + @"\WFFS.ini");
            WriteIniData("FrmZC", "Y", y, Application.StartupPath + @"\WFFS.ini");
        }
        public static bool WriteIniData(string Section, string Key, string Value, string iniFilePath)
        {
            if (File.Exists(iniFilePath))
            {
                long OpStation = WritePrivateProfileString(Section, Key, Value, iniFilePath);
                if (OpStation == 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                return false;
            }
        }
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
    }
}
